// 测试日志时间修复
const winston = require('winston');
const path = require('path');
const fs = require('fs');

// 获取北京时间的工具函数
const getBeijingTime = () => {
    const now = new Date();
    const beijingTime = new Date(now.getTime() + (8 * 60 * 60 * 1000));
    return beijingTime;
};

// 格式化北京时间为字符串
const formatBeijingTime = (date = null) => {
    const time = date || getBeijingTime();
    return time.toISOString().replace('T', ' ').replace('Z', ' +08:00');
};

// 创建测试日志记录器
const createTestLogger = () => {
    const beijingTime = getBeijingTime();
    const dateStr = beijingTime.toISOString().split('T')[0];
    const logDir = path.join(__dirname, 'logs');
    
    if (!fs.existsSync(logDir)) {
        fs.mkdirSync(logDir, { recursive: true });
    }
    
    return winston.createLogger({
        level: 'info',
        format: winston.format.combine(
            winston.format.timestamp(),
            winston.format.printf(({ timestamp, level, message }) => {
                // 将UTC时间转换为北京时间
                const utcTime = new Date(timestamp);
                const beijingTime = new Date(utcTime.getTime() + (8 * 60 * 60 * 1000));
                const beijingTimeStr = beijingTime.toISOString().replace('T', ' ').replace('Z', ' +08:00');
                return `${beijingTimeStr} [${level.toUpperCase()}] ${message}`;
            })
        ),
        transports: [
            new winston.transports.File({ 
                filename: path.join(logDir, `test-log-${dateStr}.log`) 
            }),
            new winston.transports.Console()
        ]
    });
};

// 测试日志
console.log('=== 时间测试 ===');
console.log('当前系统时间:', new Date().toISOString());
console.log('北京时间:', formatBeijingTime());

const logger = createTestLogger();

console.log('\n=== 日志测试 ===');
logger.info('这是一条测试日志消息');
logger.info('当前系统时间: ' + new Date().toISOString());
logger.info('北京时间: ' + formatBeijingTime());
logger.info('测试完成');

console.log('\n✅ 测试完成，请检查日志文件中的时间格式');
